def logtimes = []
def times = []
def movingAverages = []

//Ignores type for now: TODO
movingAverage = {ln, window , type, membershipFunction ->
	        if (!membershipFunction(ln)) return
		def matcher = ln =~ /[\[]([^\]]*)[\]]/
		if(matcher)
		{
			sdate = matcher[0][1]
			formatter = new java.text.SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss z"); 
			def date = (Date)formatter.parse(sdate) 
	                       logtimes << date
		}
	    }
//Ignore type for now
movingAverageResult = { type, window ->
        if(logtimes)
        {
	        Collections.sort(logtimes)
	
	for(date in logtimes){
		if(times.size() == 0)
		{
			times << date.getTime()
		}else
		{
			newTime = date.getTime()
			while(times.size() > 0 && newTime-times[0] > window)
			{
				times.remove(0);
			}
			times<<newTime
			if(times.size() > 1)
			{
				def deltaTime = newTime - times[0]
				if(deltaTime)
					movingAverages << times.size()*1000/deltaTime
			}
		}
	}
        logtimes = false
        }
        movingAverages
}
